import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import Detail from '@/views/Detail/index.vue'
import CartList from '@/views/CartList/index.vue'
import Checkout from '@/views/Checkout/index.vue'
import Pay from '@/views/Pay/index.vue'
import PayBack from '@/views/Pay/PayBack.vue'
import Member from '@/views/Member/index.vue'
import UserInfo from '@/views/Member/components/UserInfo.vue'
import UserOrder from '@/views/Member/components/UserOrder.vue'
import UserAddress from '@/views/Member/components/UserAddress.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // path 和 component 对应关系
  routes: [
    {
      path: '/',
      name: 'index',
      component: Layout,
      children:[
        {
          path: '',
          name: 'home',
          component: Home,
        },
        {
          path: 'category/:id',
          name: 'category',
          component: Category,
        },
        {
          path: 'category/sub/:id',
          name: 'sub-category',
          component: SubCategory,
        },
        {
          path: 'detail/:id',
          name: 'detail',
          component: Detail,
        },
        {
          path: 'cartlist',
          name: 'cart-list',
          component: CartList,
        },
        {
          path: 'checkout',
          name: 'checkout',
          component: Checkout,
        },
        {
          path: 'pay',
          name: 'pay',
          component: Pay,
        },
        {
          path: 'paycallback',
          name: 'pay-back',
          component: PayBack,
        },
        {
          path: 'member',
          name: 'member',
          component: Member,
          children: [
            // {
            //   path: '', // member的默认子路由
            //   name: 'user-info',
            //   component: UserInfo,
            // },
            // 默认跳转到user
            {
              path: '',
              name: 'member-user',
              redirect: 'member/user',
            },
            {
              path: 'user',
              name: 'user-info',
              component: UserInfo,
            },
            {
              path: 'order',
              name: 'user-order',
              component: UserOrder,
            },
            {
              path: 'address',
              name: 'user-address',
              component: UserAddress,
            }
          ]
        }
      ],
    },
    {
      path: '/login',
      name: 'login',
      component:Login,
    },
  ],
  // 路由滚动行为
  // 始终滚动到顶部
  scrollBehavior() {
    return {
      top: 0,
    }
  }
})

export default router
